package com.modules.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.Writer;

/**
 * Created by lewis on 2017/6/20.
 */
public class JavaScriptLibraryTag extends SimpleTagSupport {
    public void doTag() throws JspException,IOException
    {
        PageContext pageContext=(PageContext)this.getJspContext();

        Writer out = getJspContext().getOut();


        out.write("<script type=\"text/javascript\">");
        out.write("jQuery.ajaxSetup({cache:false});");


        out.write("function formatDate(value){");
        out.write("\tvar longValue = parseFloat(value);");
        out.write("\tif(longValue != NaN){");
        out.write("\t\tvar date = new Date(value);");
        out.write("\t\treturn $.date.format(date, $.date.DATE_FORMAT_4);");
        out.write("\t}");
        out.write("\treturn value;");
        out.write("}");
        out.write("");
        out.write("function formatDateTime(value){");
        out.write("\tvar longValue = parseFloat(value);");
        out.write("\tif(longValue != NaN){");
        out.write("\t\tvar date = new Date(value);");
        out.write("\t\treturn $.date.format(date, $.date.DATE_FORMAT_1);");
        out.write("\t}");
        out.write("\treturn value;");
        out.write("}");
        out.write("function formatCenter(value){");
        out.write("\tif(!value) return '';");
        out.write("\t\treturn '<span style=\"width:100%;text-align:center\">' + value + '</span>'");
        out.write("}");
        out.write("function formatRight(value){");
        out.write("\tif(!value) return '';");
        out.write("\t\treturn '<span style=\"width:100%;text-align:right\">' + value + '</span>'");
        out.write("}");
        out.write("function formatCommafy(value){");
        out.write("\tif(value === undefined || value == null) return '';");
        out.write("\treturn '<span style=\"width:100%;text-align:right\">' + $.format.commafy(value) + '</span>';");
        out.write("}");
        out.write("function formatCommafy2(value){");
        out.write("\tif(value === undefined || value == null) return '';");
        out.write("\treturn '<span style=\"width:100%;text-align:right\">' + $.format.roundCommafy(value, 2) + '</span>';");
        out.write("}");
        out.write("function formatCommafy4(value){");
        out.write("\tif(value === undefined || value == null) return '';");
        out.write("\treturn '<span style=\"width:100%;text-align:right\">' + $.format.roundCommafy(value, 4) + '</span>';");
        out.write("}");
        out.write("</script>");
    }
}
